import { MessagePlugin } from 'tdesign-vue-next'
import useClipboard from 'vue-clipboard3'

// 判断是否是微信端
export function isWeixin() {
  const ua = window.navigator.userAgent.toLowerCase()
  // 通过正则表达式匹配ua中是否含有MicroMessenger字符串
  if (ua.match(/MicroMessenger/i) && ua.match(/MicroMessenger/i)[0] === 'micromessenger') {
    return true
  }
  return false
}

// 判断是否是移动端
export function isMobile() {
  const ua = window.navigator.userAgent.toLowerCase()

  return !!ua.match(
    /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
  )
}

// 判断是否是PC端
export function isPc() {
  const userAgentInfo = window.navigator.userAgent
  const agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod']

  return !agents.some(agent => userAgentInfo.indexOf(agent) > 0)
}

export function isMobileInWeChat() {
  const ua = window.navigator.userAgent
  const isAndroid = /Android/i.test(ua)
  const isIPhone = /iPhone/i.test(ua)
  const isIPad = /iPad/i.test(ua)
  const isMobile = isAndroid || isIPhone || isIPad
  const screenWidth = window.screen.width
  const screenHeight = window.screen.height
  const isScreenSmall = Math.min(screenWidth, screenHeight) < 768 // 假设宽度小于 768px 的设备为手机或平板

  return isMobile && isScreenSmall
}

// 生成随机字符串
export function randomString(len = 32) {
  const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678' // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
  const maxPos = chars.length

  return Array.from({ length: len }, () => chars.charAt(Math.floor(Math.random() * maxPos))).join(
    ''
  )
}
export function getCurrentTimestamp() {
  return Math.floor(new Date().getTime() / 1000)
}

export const clearStorage = () => {
  localStorage.removeItem('orderAllList')
  localStorage.removeItem('orderCanceledList')
  localStorage.removeItem('orderPaidList')
  localStorage.removeItem('orderRefundedList')
}

export const copyToClipboard = async text => {
  const { toClipboard } = useClipboard()
  if (!text) {
    MessagePlugin.error('请您先登录!')
  }
  try {
    await toClipboard(text).then(res => {
      if (res.text) {
        MessagePlugin.success('复制成功!')
      }
      //console.log(res)
    })
  } catch (e) {
    //console.log(e)
  }
}
export const getUrl = text => {
  return import.meta.env.VITE_WEB_URL + '?uid=' + text
}

// //使用原生JavaScript方法监听屏幕宽度
// export const resizeWidth = () => {
//   return (() => {
//     const mediaQuery = window.matchMedia('(max-width: 768px)');
//     const handleScreenChange = e => {
//       if (e.matches) {
//         //console.log('小于等于768');
//         return true;
//       } else {
//         //console.log('大于768');
//         return false;
//       }
//     }
//     mediaQuery.addEventListener('change', handleScreenChange);
//     return mediaQuery.matches ? true : false;
//   })();
// }
export const checkIsMobile = () => {
  const isMobile = window.innerWidth <= 768
  return isMobile
}

export const watchIsMobile = callback => {
  let isMobile = checkIsMobile()
  const handleResize = () => {
    const newIsMobile = checkIsMobile()
    if (isMobile !== newIsMobile) {
      isMobile = newIsMobile
      callback(isMobile)
    }
  }
  window.addEventListener('resize', handleResize)
  return () => window.removeEventListener('resize', handleResize)
}

export const getTicketClass = () => {
  return JSON.parse(sessionStorage.getItem('ticketClassList'))
}
